if (isdedicated || !hasInterface) exitwith {};

RMM_task_lines = ["none","created","assigned","succeeded","failed","canceled"];

[] spawn {
	if (!isnil "persistentDBHeader") then {
		if (persistentDBHeader == 1) then {
			
			waitUntil{MISSIONDATA_LOADED == "true"};

			if (pdb_tasks_enabled) then {
				//diag_log format ["I'm waiting for tasks %1",time];
				waitUntil{!isNil "RMM_tasks"};
				//diag_log format ["Ok tasks are now loaded %1:[%2]",time, RMM_tasks];
				diag_log format["Loaded RMM Tasks, %1, %2", RMM_tasks, count RMM_tasks];
			};
		
		};
	};
	
	RMM_mytasks = [];
	if (isnil "RMM_tasks") then {
			RMM_tasks = [];
			publicvariable "RMM_tasks";
	} else {
			{
					diag_log format["Adding RMM Task %1", _x];
				RMM_mytasks set [count RMM_mytasks, _x call tasks_fnc_taskAdd];
			} foreach RMM_tasks;
	};

	if (!isDedicated) then {
		if (isNil "TYR_MENU_IsActive" || {TYR_MENU_IsActive <= 0}) then {
			["player", [mso_interaction_key], -9402, ["tmf\support\modules\rmm_tasks\fn_menuDef.sqf", "main"]] call CBA_ui_fnc_add;
			[localize "STR_TASKS_CAPTION","openmap true; if ((call mso_fnc_hasRadio) && ([(getPlayerUID player),['leader','admin']] call mp_rights_fnc_hasRoles)) then {call tasks_fnc_click};"] call mso_core_fnc_updateMenu;
		} else {
			if (!isnil "tyr_fnc_addHandler") then {
				["TYRMENU",
					[[
						localize "STR_TASKS_CAPTION"
						,{[(getPlayerUID player),['leader','admin','tasks']] call mp_rights_fnc_hasRoles}
						,{openmap true; call tasks_fnc_click;}
						,"tmf\support\modules\rmm_tasks\tasks.paa"
					]]
				] call tyr_fnc_addHandler;
			};
		};
		["RMM_tasks__Global_EVT_ID", "onMapSingleClick", {
			if (_shift && _alt && ([(getPlayerUID player),['leader','admin']] call mp_rights_fnc_hasRoles)) then {
				RMM_tasks_position = _pos;
				RMM_tasks_position set [2,0];
				createDialog "RMM_ui_tasks";
			};
		}] call BIS_fnc_addStackedEventHandler;
	};

	"RMM_tasks" addPublicVariableEventHandler {
		{
			if(str (_x select 2) == "[0,0,0]" && playerSide == (_x select 4)) then {
				private ["_taskname","_description","_destination","_playerSide","_state"];
				_taskname = _x select 0;
				_description = _x select 1;
				_destination = _x select 2;
				_state = _x select 3;
				_playerSide = _x select 4;

				if (_playerSide == playerSide) then {
					private "_task";
					_task = player createsimpletask [_taskname];
					_task setsimpletaskdescription _description;
					_task setsimpletaskdestination _destination;
					_task settaskstate _state;
					missionnamespace setvariable [_taskname,_task];

					RMM_mytasks set [count RMM_mytasks, _task];
				};
			};
		} forEach (_this select 1);
	};
};
